/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.emergency.plan.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.validate.group.AuditGroup;
import com.hopes.bizz.emergency.plan.dto.ErGovernmentEmergencyPlanDTO;
import com.hopes.bizz.emergency.plan.entity.EmergencyParkPlanEntity;
import com.hopes.bizz.emergency.plan.vo.ErGovernmentEmergencyPlanExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.emergency.plan.entity.ErGovernmentEmergencyPlanEntity;
import com.hopes.bizz.emergency.plan.service.ErGovernmentEmergencyPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * 政府应急预案
 *
 * @author cx
 * @date 2023-10-25 17:34:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/plan/government" )
@Tag(description = "government" , name = "政府应急预案管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ErGovernmentEmergencyPlanController {

    private final  ErGovernmentEmergencyPlanService erGovernmentEmergencyPlanService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param erGovernmentEmergencyPlan 政府应急预案
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('plan_government_view')" )
    public R geterGovernmentEmergencyPlanPage(Page page, ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlan) {
        LambdaQueryWrapper<ErGovernmentEmergencyPlanEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(erGovernmentEmergencyPlan.getPlanNo()),ErGovernmentEmergencyPlanEntity::getPlanNo,erGovernmentEmergencyPlan.getPlanNo());
		wrapper.like(StrUtil.isNotBlank(erGovernmentEmergencyPlan.getPlanName()),ErGovernmentEmergencyPlanEntity::getPlanName,erGovernmentEmergencyPlan.getPlanName());
		wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getPlanLevel()),ErGovernmentEmergencyPlanEntity::getPlanLevel,erGovernmentEmergencyPlan.getPlanLevel());
		wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getPlanType()),ErGovernmentEmergencyPlanEntity::getPlanType,erGovernmentEmergencyPlan.getPlanType());
		wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getIndustryType()),ErGovernmentEmergencyPlanEntity::getIndustryType,erGovernmentEmergencyPlan.getIndustryType());
        return R.ok(erGovernmentEmergencyPlanService.page(page, wrapper));
    }


    /**
     * 通过id查询政府应急预案
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('plan_government_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(erGovernmentEmergencyPlanService.getById(id));
    }

    /**
     * 新增政府应急预案
     * @param erGovernmentEmergencyPlan 政府应急预案
     * @return R
     */
    @Operation(summary = "新增政府应急预案" , description = "新增政府应急预案" )
    @SysLog("新增政府应急预案" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('plan_government_add')" )
    public R save(@RequestBody ErGovernmentEmergencyPlanDTO erGovernmentEmergencyPlan) {
        erGovernmentEmergencyPlan.setEffectiveDate(LocalDate.now());
        return R.ok(erGovernmentEmergencyPlanService.saveGovernment(erGovernmentEmergencyPlan));
    }

    /**
     * 修改政府应急预案
     * @param erGovernmentEmergencyPlan 政府应急预案
     * @return R
     */
    @Operation(summary = "修改政府应急预案" , description = "修改政府应急预案" )
    @SysLog("修改政府应急预案" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('plan_government_edit')" )
    public R updateById(@RequestBody ErGovernmentEmergencyPlanDTO erGovernmentEmergencyPlan) {
        return R.ok(erGovernmentEmergencyPlanService.updateGovernment(erGovernmentEmergencyPlan));
    }

    /**
     * 通过id删除政府应急预案
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除政府应急预案" , description = "通过id删除政府应急预案" )
    @SysLog("通过id删除政府应急预案" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('plan_government_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(erGovernmentEmergencyPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param erGovernmentEmergencyPlan 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('plan_government_export')" )
    public List<ErGovernmentEmergencyPlanExcelVO> export(ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlan) {
        LambdaQueryWrapper<ErGovernmentEmergencyPlanEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(erGovernmentEmergencyPlan.getPlanNo()),ErGovernmentEmergencyPlanEntity::getPlanNo,erGovernmentEmergencyPlan.getPlanNo());
        wrapper.like(StrUtil.isNotBlank(erGovernmentEmergencyPlan.getPlanName()),ErGovernmentEmergencyPlanEntity::getPlanName,erGovernmentEmergencyPlan.getPlanName());
        wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getPlanLevel()),ErGovernmentEmergencyPlanEntity::getPlanLevel,erGovernmentEmergencyPlan.getPlanLevel());
        wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getPlanType()),ErGovernmentEmergencyPlanEntity::getPlanType,erGovernmentEmergencyPlan.getPlanType());
        wrapper.eq(Objects.nonNull(erGovernmentEmergencyPlan.getIndustryType()),ErGovernmentEmergencyPlanEntity::getIndustryType,erGovernmentEmergencyPlan.getIndustryType());
        return erGovernmentEmergencyPlanService.exportExcel(erGovernmentEmergencyPlanService.list(wrapper));
    }

    @Operation(summary = "提交预案审核", description = "提交预案审核")
    @SysLog("提交预案审核")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated(AuditGroup.class) @RequestBody ErGovernmentEmergencyPlanEntity erGovernmentEmergencyPlan) {
        erGovernmentEmergencyPlanService.handlerAudit(erGovernmentEmergencyPlan);
        return R.ok();
    }
}